package com.sjgame.model.controller;

import com.sjgame.base.common.JsonResult;
import com.sjgame.base.common.PageByBootStarp;
import com.sjgame.base.common.utils.MD5Utils;
import com.sjgame.base.controller.BaseController;
import com.sjgame.model.entity.AgentUser;
import com.sjgame.model.service.AgentUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * create by zcl
 */
@Controller
@RequestMapping("/agent")
public class AgentUserController extends BaseController {
    @Autowired
    private AgentUserService agentUserService;
    @RequestMapping(value = {"/index"})
    public String index(ModelMap map,String account) {
        return "agent/index";
    }

    @RequestMapping(value = {"/indexdata"})
    @ResponseBody
    public PageByBootStarp indexdata(ModelMap map, String id) {
        Sort t  = new Sort(Sort.Direction.DESC,"id");
        Page<AgentUser> page = agentUserService.findAll(new Specification<AgentUser>() {
            @Override
            public Predicate toPredicate(Root<AgentUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namepath = root.get("createName");
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(namepath,getUser().getUserName()));
                if(!StringUtils.isEmpty(id)) {
                    list.add(cb.equal(root.get("id"),id));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },getPageRequest(t));

        return new PageByBootStarp().formatResult(page);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "agent/form";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, ModelMap map) {
        AgentUser user = agentUserService.find(id);
        map.put("user", user);
        return "agent/form";
    }

    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editAgentUser(AgentUser user, ModelMap map) {
        try {

            if(org.springframework.util.StringUtils.isEmpty(user.getId())){
                user.setCreateName(this.getUser().getUserName());
                user.setUpdateTime(new Date());
                user.setOperator(this.getUser().getNickName());
                user.setStatus("0");
                user.setPassword(MD5Utils.md5(user.getPhone().substring(user.getPhone().length()-4, user.getPhone().length())));
                agentUserService.save(user);
            }else{
                AgentUser user2 = agentUserService.find(user.getId());
                //user2.setStatus(user.getStatus());
                user2.setIdCard(user.getIdCard());
                user2.setNickname(user.getNickname());
                user2.setPhone(user.getPhone());
                user2.setWechat(user.getWechat());
                user2.setOperator(this.getUser().getNickName());
                user2.setUpdateTime(new Date());
                user2.setDescription(user.getDescription());
                agentUserService.update(user2);
            }


        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteAgentUser(@PathVariable Integer id, ModelMap map) {
        try {
            agentUserService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure("删除失败");
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/fenghao/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult FengHaoAgentUser(@PathVariable Integer id, ModelMap map, String status) {
        try {
            AgentUser user2 = agentUserService.find(id);
            user2.setStatus(status);
            user2.setOperator(this.getUser().getNickName());
            user2.setUpdateTime(new Date());
            agentUserService.update(user2);

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure("删除失败");
        }
        return JsonResult.success();
    }

    @RequestMapping(value = {"/checkphone"})
    @ResponseBody
    public JsonResult checkUserName(String phone, ModelMap modelMap) {
        try {
            AgentUser user = agentUserService.findByPhone(phone);
            if(user!=null)return JsonResult.failure("电话号码已存在");
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

}
